package com.prompt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class SetAdapter extends BaseAdapter{

	private List<Boolean> mChecked;  
	private List<SetVO> listSet;
    private Context context = null;
    private HashMap<Integer,View> map = new HashMap<Integer,View>();   
      
    public SetAdapter(Context context, List<SetVO> list){  
    	this.context = context;
//        listSet = new ArrayList<SetVO>();  
        listSet = list;  
          
        mChecked = new ArrayList<Boolean>();  
        for(int i=0;i<list.size();i++){  
            mChecked.add(list.get(i).getSelect());  
        }  
    }  

    @Override  
    public int getCount() {  
        return listSet.size();  
    }  

    @Override  
    public Object getItem(int position) {  
        return listSet.get(position);  
    }  

    @Override  
    public long getItemId(int position) {  
        return position;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        View view;  
        final ViewHolder holder;  
          
        if (convertView == null) {  //map.get(position) == null
            Log.e("SetAdapter","position1 = "+position);  
              
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            view = mInflater.inflate(R.layout.activity_main_set_list_item, null);  
            holder = new ViewHolder();  
            holder.selected = (CheckBox)view.findViewById(R.id.cbSet);  
            holder.title = (TextView)view.findViewById(R.id.mainSetItemText);  
            holder.btSet = (Button)view.findViewById(R.id.btSet);  
            map.put(position, view);  
                       
            view.setTag(holder);  
        }else{  
            Log.e("SetAdapter","position2 = "+position);  
            view = convertView;//map.get(position);  
            holder = (ViewHolder)view.getTag();  
        }
          
        holder.selected.setChecked(mChecked.get(position));  
        holder.title.setText(listSet.get(position).getTitle());  
        final int pos = position;
        
        OnClickListener listener=new OnClickListener(){  
            @Override  
            public void onClick(View v)  {  
                if(v==holder.btSet){  
                    Toast.makeText(context,"跳到设置项"+pos, Toast.LENGTH_LONG).show();
                }  
                
            }  
        };  
        holder.btSet.setOnClickListener(listener);  
//        Button btn=(Button)view.findViewById(R.id.btSet);
//        Button.OnClickListener setOnClickListener = new Button.OnClickListener()
//        {
//            public void onClick(View v) {
//            	Toast.makeText(context,"跳到设置项"+pos, Toast.LENGTH_LONG).show();
//            }
//        };
//        btn.setOnClickListener(setOnClickListener); 
            
        return view;  
    }  
    
    class ViewHolder{  
        CheckBox selected;  
        TextView title;  
        Button btSet;  
    }  
}
